<?php
require_once 'lib/controllerWithoutGetOrPost.php';
require_once 'view/standard-personal-noleftbar-top.php';
function commentNames($firstname, $lastname)
{
    if (! empty($firstname) && ! empty($lastname))
    {
        return $firstname." ".$lastname.". ";
    } else if (! empty($firstname))
    {
        return $firstname.". ";
    } else if (! empty($lastname))
    {
        return $lastname.". ";
    } else
    {
        return "";
    }
}

function commentCountry($city, $country)
{
    if (! empty($city) && ! empty($country))
    {
        return $city.", ".$country.". ";
    } else if (! empty($city))
    {
        return $city.". ";
    } else if (! empty($country))
    {
        return $country.". ";
    } else
    {
        return "";
    }
}

function pageNavLink($totalpages, $pagenumber, $currentpage, $personid)
{
    if ($pagenumber <= $totalpages)
    {
        if ($currentpage != $pagenumber)
        {
            return "<a href=\"remembrance.php?personid=".$personid."&page=".$pagenumber."\">".$pagenumber."</a>\n\t";
        } else
        {
            return "<strong>[ ".$pagenumber." ]</strong>\n\t";
        }
    }
}

function insertSearchPageTopNavigation($personid, $totalpages, $currentpage)
{

    $startpage = $currentpage;
    if ($currentpage < 3)
    {
        $startpage = 3;
    }

    $page1 = $startpage-2;
    $page2 = $startpage-1;
    $page3 = $startpage;
    $page4 = $startpage+1;
    $page5 = $startpage+2;

    echo "<li class=\"sortby\">";
    echo "<a class=\"leftfloat\" href=\"leaveamessage.php?personid=".$personid."\">Leave a Message</a>\n\t";
    echo "<a href=\"remembrance.php?personid=".$personid."&page=".$totalpages."\">&lt;&lt;</a>\n\t";
    echo pageNavLink($totalpages, $page1, $currentpage, $personid);
    echo pageNavLink($totalpages, $page2, $currentpage, $personid);
    echo pageNavLink($totalpages, $page3, $currentpage, $personid);
    echo pageNavLink($totalpages, $page4, $currentpage, $personid);
    echo pageNavLink($totalpages, $page5, $currentpage, $personid);
    echo "...\n\t";
    echo "<a href=\"remembrance.php?personid=".$personid."&page=".$totalpages."\">".$totalpages."</a>\n\t";
    echo "<a href=\"remembrance.php?personid=".$personid."&page=".$totalpages."\">&gt;&gt;</a>\n\t";
    echo "</li>";


}

function insertSearchPageBottomNavigation($personid, $totalpages, $currentpage)
{

    $startpage = $currentpage;
    if ($currentpage < 3)
    {
        $startpage = 3;
    }

    $page1 = $startpage-2;
    $page2 = $startpage-1;
    $page3 = $startpage;
    $page4 = $startpage+1;
    $page5 = $startpage+2;

    echo "<li class=\"sortby\">";
    echo "<a href=\"#maincontainer\" title=\"Back to Top\" class=\"leftfloat\">Back to Top</a>\n\t";
    echo "<span class=\"leftfloat\">All messages submitted are read by our staff before being published.</span>";
    echo "<a href=\"remembrance.php?personid=".$personid."&page=1\">&lt;&lt;</a>\n\t";
    echo pageNavLink($totalpages, $page1, $currentpage, $personid);
    echo pageNavLink($totalpages, $page2, $currentpage, $personid);
    echo pageNavLink($totalpages, $page3, $currentpage, $personid);
    echo pageNavLink($totalpages, $page4, $currentpage, $personid);
    echo pageNavLink($totalpages, $page5, $currentpage, $personid);
    echo "...\n\t";
    echo "<a href=\"remembrance.php?personid=".$personid."&page=".$totalpages."\">".$totalpages."</a>\n\t";
    echo "<a href=\"remembrance.php?personid=".$personid."&page=".$totalpages."\">&gt;&gt;</a>\n\t";
    echo "</li>";

}

function convertDateTime($value)
{


    $blah = substr($value, 6, 4); // bcdef
    $blah = $blah."-".substr($value, 3, 2);
    $blah = $blah."-".substr($value, 0, 2);

    return $blah;


}



function IncrementFlowers($id, $dblink)
{

    $queryflowers = "Select `Count` from RemembranceFlowers where `PersonID` = ".mysql_real_escape_string($id);

    $resultflowers = mysql_query($queryflowers, $dblink);

    // Check result
    // This shows the actual query sent to MySQL, and the error. Useful for debugging.
    if (!$resultflowers)
    {
        $message = 'Invalid query: '.mysql_error()."\n";
        //$message .= 'Whole query: ' . $queryflowers;
        die ($message);
    }

    if (mysql_num_rows($resultflowers) != 0)
    {
        $queryupdate = "Update RemembranceFlowers Set `Count` = `Count` + 1 Where `PersonID` = ".mysql_real_escape_string($id);
        $resultupdate = mysql_query($queryupdate, $dblink);
        if (!$resultupdate)
        {
            $message = 'Invalid query: '.mysql_error()."\n";
            //$message .= 'Whole query: ' . $queryupdate;
            die ($message);
        }
    } else
    {
        $queryinsert = "Insert Into RemembranceFlowers (`PersonID`, `Count`) VALUES ('".$id."', 1)";
        $resultinsert = mysql_query($queryinsert, $dblink);
        if (!$resultinsert)
        {
            $message = 'Invalid query: '.mysql_error()."\n";
            //$message .= 'Whole query: ' . $queryinsert;
            die ($message);
        }
    }

    mysql_free_result($resultflowers);

}

function IncrementCandles($id, $dblink)
{


    $querycandle = "Select `Count` from RemembranceCandle where `PersonID` = ".mysql_real_escape_string($id);

    $resultcandle = mysql_query($querycandle, $dblink);


    // Check result
    // This shows the actual query sent to MySQL, and the error. Useful for debugging.
    if (!$resultcandle)
    {
        $message = 'Invalid query: '.mysql_error()."\n";
        //$message .= 'Whole query: ' . $querycandle;
        die ($message);
    }

    if (mysql_num_rows($resultcandle) != 0)
    {
        $queryupdate = "Update RemembranceCandle Set `Count` = `Count` + 1 Where `PersonID` = ".mysql_real_escape_string($id);
        $resultupdate = mysql_query($queryupdate, $dblink);
        if (!$resultupdate)
        {
            $message = 'Invalid query: '.mysql_error()."\n";
            //$message .= 'Whole query: ' . $queryupdate;
            die ($message);
        }
    } else
    {
        $queryinsert = "Insert Into RemembranceCandle (`PersonID`, `Count`) VALUES ('".$id."', 1)";
        $resultinsert = mysql_query($queryinsert, $dblink);
        if (!$resultinsert)
        {
            $message = 'Invalid query: '.mysql_error()."\n";
            //$message .= 'Whole query: ' . $queryinsert;
            die ($message);
        }
    }

    mysql_free_result($resultcandle);

}





function FirstNames($value)
{
    if ( isset ($value) && $value != "")
    {
        return "<li><span>First Names: </span>".stripslashes($value)."</li>";
    }
}

function KnownAs($value)
{
    if ( isset ($value) && $value != "")
    {
        return "<li><span>Known as:</span>".stripslashes($value)."</li>";
    }
}

function LastName($value)
{
    if ( isset ($value) && $value != "")
    {
        return "<li><span>Last Name:</span>".stripslashes($value)."</li>";
    }
}

function Nee($value)
{
    if ( isset ($value) && $value != "")
    {
        return " nee ".$value;
    }
    return "";
}

function OtherNames($value, $nee)
{
    if (( isset ($value) && $value != "") || ( isset ($nee) && $nee != ""))
    {
        return "<li><span>Other Names:</span>".$value.Nee($nee)."</li>";
    }
}

function formatdatetime($date)
{
    if ($date == "0000-00-00 00:00:00")
    {
        return "";
    } else
    {
        return date('d F Y', strtotime(str_replace("&", "&amp;", $date)));
    }
}

function formatdatetime4($date)
{
    if ($date == "0000-00-00 00:00:00")
    {
        return "";
    } else
    {
        return date('d M Y', strtotime(str_replace("&", "&amp;", $date)));
    }
}

function BirthDate($value)
{
    if ( isset ($value) && $value != "" && $value != "0000-00-00 00:00:00")
    {
        return "<li><span>Birth Date: </span>".formatdatetime($value)."</li>";
    }
}

function DeathDate($value)
{
    if ( isset ($value) && $value != "" && $value != "0000-00-00 00:00:00")
    {
        return "<li><span>Death Date: </span>".formatdatetime($value)."</li>";
    }
}

function Age($value, $measure)
{
    if ( isset ($value) && $value != "" && $value != "0")
    {
        return "<li><span>Age: </span>".$value.$measure."</li>";
    }
}

function Country($value)
{
    if ( isset ($value) && $value != "")
    {
        return "<li><span>Country: </span>".$value."</li>";
    }
}

function Funeral($id, $value, $dblink)
{
    if ( isset ($value) && $value != "")
    {
        $query = "select FuneralDetails.`Web` from FuneralDetails left join FuneralHome on FuneralHome.ID = FuneralDetails.FuneralHomeID where FuneralHomeID = ".$id;
        $result = mysql_query($query, $dblink);

        // Check result
        // This shows the actual query sent to MySQL, and the error. Useful for debugging.
        if (!$result)
        {
            $message = 'Invalid query: '.mysql_error()."\n";
            //$message .= 'Whole query: ' . $query2;
            die ($message);
        }
        $returnval = "";
        if (mysql_num_rows($result) != 0)
        {
            $val = mysql_result($result, 0);
            if ($val != "" && isset ($val))
            {
                $returnval = "<li><span>Funeral Home:</span> <a href=\"".$val."\" title=\"\">".$value."</a></li>";
            } else
            {
                $returnval = "<li><span>Funeral Home:</span> ".$value."</a></li>";
            }
        } else
        {
            $returnval = "<li><span>Funeral Home:</span> ".$value."</a></li>";
        }

        mysql_free_result($result);

        return $returnval;


    }
}

function Charity($value, $http)
{
    if (! empty($value)) {
        $charity = $value;
        if (! empty($http)) {
            $charity = '<a href="'.$http.'">'.$value.'</a>';
        }
        echo '<li><span>Charity:</span> '.$charity.'</li>';
    }
}

function PublishedNotices($id, $dblink)
{
    if (!empty($id)) {
        $query = "select count(*) from Publication left join PublicationPerson on Publication.ID = PublicationPerson.PublicationID where PublicationPerson.PersonID = ".$id;
        $result = mysql_query($query, $dblink);
        $published = '(0)';
        if (mysql_result($result, 0)) {
            $published = '(<a href="personnotices.php?personid='.$id.'">'.mysql_result($result, 0).'</a>) Newspapers - <a href="personnotices.php?personid='.$id.'">view details</a>';
        }
        $notices = '<li><span>Published Notices:</span> '.$published.'</li>';
        mysql_free_result($result);
        return $notices;
    }
}
            if ( isset ($_GET['personid']) && $_GET['personid'] != "")
            {
                if ( isset ($_GET['lightcandle']) && $_GET['lightcandle'] = "1")
                {
                    IncrementCandles($_GET['personid'], $link);
            
                }
                if ( isset ($_GET['refreshflowers']) && $_GET['refreshflowers'] = "1")
                {
                    IncrementFlowers($_GET['personid'], $link);
            
                }
            
                $query = "SELECT Person.ID, 
                                                Person.Surname, 
                                                Person.FirstNames, 
                                                Person.FormerName, 
                                                Person.NeeName, 
                                                Person.AlsoKnownAs, 
                                                Person.DeathDate, 
                                                Person.BirthDate, 
                                                Person.Age AS 'Age', 
                                                Person.AgeMeasure AS 'AgeMeasure', 
                                                CityTown.Name as 'CityTown', 
                                                c2.Name as 'FormerCityTown', 
                                                Region.Name AS 'Region', 
                                                Country.Name AS 'Country', 
                                                Charity.Name AS 'Charity', 
                                                Charity.Web AS 'Web',
                                                FuneralHome.ID AS 'FuneralID',
                                                FuneralHome.Name AS 'FuneralHome',
                                                Person.OtherInfo As 'Other Information'
                                            FROM Person 
                                                left join Region on Person.RegionID = Region.ID 
                                                left join Country on Country.ID = Person.CountryID 
                                                left join CityTown AS c2 on Person.FormerCityTownID = c2.ID 
                                                left join Charity on Charity.ID = Person.CharityID 
                                                left join FuneralHome on FuneralHome.ID = Person.FuneralHomeID 
                                                left join CityTown on CityTown.ID = Person.CityTownID
                                            WHERE Person.ID = ".mysql_real_escape_string($_GET['personid']);
            
                $result = mysql_query($query, $link);
                if (mysql_num_rows($result) != 0)
                {
                    $row = mysql_fetch_assoc($result);
        ?>
        <div id="contentarea">
            <ul id="fullbar">
                <li id="profileinfo">
                    <div class="profileimage">
                        <?php
                        //<a href="#" title=""><img src="images/assets/silhouette.jpg" alt="Profilepic Placeholder" /></a>
                        
                        $image = "/home/amemoryt/public_html/images/featuring/".$row['ID'].".jpg";
                        if (file_exists($image))
                        {
                            echo "<img alt=\"Profilepic Placeholder\" style=\"width:120px;\" src=\"http://amemorytree.co.nz/images/featuring/".$row['ID'].".jpg\"/>";
                        } else
                        {
                            echo "<img src=\"images/assets/silhouette.jpg\" alt=\"Profilepic Placeholder\" />";
                        }
                        
                        ?>
                        <?php
                        //<a href="#" title="Click to Enlarge" class="profileimage_text">Click to Enlarge</a>
                        ?>
                    </div>
                    <?php
                    
                    echo "<ul>";
                    
                    echo FirstNames($row['FirstNames']);
                    echo KnownAs($row['AlsoKnownAs']);
                    echo LastName($row['Surname']);
                    echo OtherNames($row['FormerName'], $row['NeeName']);
                    echo BirthDate($row['BirthDate']);
                    echo DeathDate($row['DeathDate']);
                    echo Age($row['Age'], $row['AgeMeasure']);
                    echo Country($row['Country']);
                    echo Funeral($row['FuneralID'], $row['FuneralHome'], $link);
                    echo Charity($row['Charity'], $row['Web']);
                    echo PublishedNotices($row['ID'], $link);
                    
                    echo "</ul>";
                    }
                    
                    $queryflowers = "Select `Count` from RemembranceFlowers where PersonID = ".mysql_real_escape_string($_GET['personid']);
                    
                    $querycandle = "Select `Count` from RemembranceCandle where PersonID = ".mysql_real_escape_string($_GET['personid']);
                    
                    $resultflowers = mysql_query($queryflowers, $link);
                    $resultcandle = mysql_query($querycandle, $link);
                    $flowers = 0;
                    if (mysql_num_rows($resultflowers) != 0)
                    {
                        $flowers = mysql_result($resultflowers, 0);
                    }
                    
                    $candle = 0;
                    if (mysql_num_rows($resultcandle) != 0)
                    {
                        $candle = mysql_result($resultcandle, 0);
                    }
                    
                    mysql_free_result($resultcandle);
                    mysql_free_result($resultflowers);
                    
                    $personId = intval($_GET['personid']);
                    $flowersLink = 'remembrance.php?personid='.$personId.'&refreshflowers=1';
                    $flowersCount = 17;
                    $flowerToUse = $flowers % $flowersCount;
                    $flowerImage = 'images/refreshflowers/'.$flowerToUse.'.jpg';
                    $flowerSize = getimagesize($flowerImage);
                    $flowerWidth = $flowerSize[0];
                    $flowerHeight = $flowerSize[1];
                    // existing page design means we can't use a span around the number of flowers
                    echo <<< EOT
                        <div class="refreshroses">
                            <a id="refreshFlowersImage" href="$flowersLink" title="Refresh Flowers">
                                <img src="$flowerImage" width="$flowerWidth" height="$flowerHeight" />
                            </a>
                            <a id="refreshFlowersText" href="$flowersLink" class="refreshroses_text" title="Refresh Flowers">
                                Refresh Flowers ($flowers)
                            </a>
                            <a id="refreshCandles" href="remembrance.php?personid=$personId&lightcandle=1" title="Light a Candle" class="lightcandle">Light a Candle ($candle)</a>
                        </div>

EOT;
                    $queryfeaturingperson = "Select `Blurb` from FeaturingPerson where PersonID = ".mysql_real_escape_string($_GET['personid']);
                    
                    $resultfeaturingperson = mysql_query($queryfeaturingperson, $link);
                    if (mysql_num_rows($resultfeaturingperson) != 0)
                    {
                        while ($rowfeaturingperson = mysql_fetch_assoc($resultfeaturingperson))
                        {
                            echo "<p>".nl2br(stripslashes($rowfeaturingperson['Blurb']))."</p>";
                        }
                    }
                    
                    mysql_free_result($resultfeaturingperson);
                    
                    
                    
                    
                    
                    //	<p>This page will remain open until 1 January 2009, <a href="" title="sponsor this page">sponsor this page</a> to keep it open.</p>
                    }
                    
                    ?>
                </li>
                <li id="profilecomments">
                    <ul id="searchresults">
                        <li class="header">
                            <h2>Messages</h2>
                            <?php
                            
                            $messagequery = "Select count(*) from RemembranceComment where PersonID = '".mysql_real_escape_string($_GET['personid'])."' AND Category = '2'";
                            $countmessage = mysql_query($messagequery, $link);
                            $entries = 0;
                            if (mysql_num_rows($countmessage) != 0)
                            {
                                $entries = mysql_result($countmessage, 0);
                            }
                            
                            echo "<span>Entries (".$entries.")</span>";
                            
                            
                            
                            $querycomment = "select 	RemembranceComment.ID,
                                                                                            RemembranceComment.PersonID, 
                                                                                                RemembranceComment.Surname, 
                                                                                                Country.Name as `Country`,
                                                                                                RemembranceComment.FirstName, 
                                                                                                RemembranceComment.ClipartID,
                                                                                                RemembranceComment.Heading, 
                                                                                                RemembranceComment.Comment, 
                                                                                                RemembranceComment.CityTown, 
                                                                                                RemembranceComment.DateAdded,
                                                                                                RemembranceComment.DateModified,
                                                                                                RemembranceComment.Email,
                                                                                                RemembranceComment.Category,
                                                                                                RemembranceComment.Heading from RemembranceComment left join Country on RemembranceComment.CountryID = Country.ID Where PersonID = '".mysql_real_escape_string($_GET['personid'])."' AND Category = '2' order by DateAdded DESC LIMIT ";
                            
                            
                            $current = 0;
                            $blah = "";
                            
                            
                            
                            $querycomment .= ' '.$blah;
                            
                            $resultcount = mysql_result($countmessage, 0);
                            
                            $totalpages = 1;
                            $resultsperpage = 10;
                            
                            $totalpages = ceil($resultcount/$resultsperpage);
                            
                            if ($totalpages < 1)
                            {
                                $totalpages = 1;
                            }
                            
                            $from = 0;
                            $to = $resultsperpage;
                            $currentpage = 1;
                            
                            if ( isset ($_GET['page']))
                            {
                                $currentpage = $_GET['page'];
                                $to = $_GET['page']*$resultsperpage;
                                $from = $to-$resultsperpage;
                                $querycomment = $querycomment.$from.",".$resultsperpage;
                            
                            } else
                            {
                                $querycomment = $querycomment."0,".$resultsperpage;
                            }
                            
                            $pagenumber = $_GET['page'];
                            
                            
                            if ($_GET['page'] > 0)
                            {
                                if ($_GET['page'] < $totalpages)
                                {
                                    $pagenumber = $_GET['page']+1;
                                }
                            } else
                            {
                                $pagenumber = $_GET['page']+2;
                            }
                            
                            $resultcomment = mysql_query($querycomment, $link);
                            $class1 = "odd";
                            $class2 = "even";
                            $class = $class1;
                            
                            
                            
                            
                            
                            ?>
                        </li>
                        <?php
                        
                        insertSearchPageTopNavigation($_GET['personid'], $totalpages, $currentpage);
                        
                        
                        echo "<li class=\"results\">
                                                                                                            <ul>";
                        
                        if (! empty($_GET['commentid']))
                        {
                            $commentq = "select 	RemembranceComment.ID,
                                                                                                                RemembranceComment.PersonID, 
                                                                                                                RemembranceComment.Surname, 
                                                                                                                Country.Name as `Country`,
                                                                                                                RemembranceComment.FirstName, 
                                                                                                                RemembranceComment.ClipartID,
                                                                                                                RemembranceComment.Heading, 
                                                                                                                RemembranceComment.Comment, 
                                                                                                                RemembranceComment.CityTown, 
                                                                                                                RemembranceComment.DateAdded,
                                                                                                                RemembranceComment.DateModified,
                                                                                                                RemembranceComment.Email,
                                                                                                                RemembranceComment.Category,
                                                                                                                RemembranceComment.Heading from RemembranceComment left join Country on RemembranceComment.CountryID = Country.ID Where RemembranceComment.ID = '".mysql_real_escape_string($_GET['commentid'])."' AND Category = '2'";
                            $commentqueryspecial = mysql_query($commentq, $link);
                        
                            if (!$commentqueryspecial)
                            {
                                $message = 'Invalid query: '.mysql_error()."\n";
                                $message .= 'Whole query: '.$commentq;
                                die ($message);
                            }
                        
                        
                            while ($rowcommentr = mysql_fetch_assoc($commentqueryspecial))
                            {
                                echo "<li class=\"ieclear\">
                                                                                                                    <div>
                                                                                                                        <h3>".$rowcommentr['Heading']."</h3>";
                                if ($rowcommentr['ClipartID'] != "" && $rowcommentr['ClipartID'] != null && $rowcommentr['ClipartID'] != 0)
                                {
                                    echo "<img src=\"images/clipart/".$rowcommentr['ClipartID'].".jpg\" class=\"leftpic\" />";
                                }
                                echo "<p>".nl2br(stripslashes($rowcommentr['Comment']))."</p></div>";
                        
                                echo "<p class=\"author\"><span>".commentNames($rowcommentr['FirstName'], $rowcommentr['Surname']).commentCountry($rowcommentr['CityTown'], $rowcommentr['Country'])."</span>".date('l, F d, Y', strtotime(str_replace("&", "&amp;", $rowcommentr['DateAdded'])))."</p></li>";
                            }
                        }
                        
                        
                        
                        while ($rowcomment = mysql_fetch_assoc($resultcomment))
                        {
                        
                        
                            echo "<li class=\"ieclear\">
                                                                                                                    <div>
                                                                                                                        <h3>".stripslashes($rowcomment['Heading'])."</h3>";
                            if ($rowcomment['ClipartID'] != "" && $rowcomment['ClipartID'] != null && $rowcomment['ClipartID'] != 0)
                            {
                                echo "<img src=\"images/clipart/".$rowcomment['ClipartID'].".jpg\" class=\"leftpic\" />";
                            }
                            echo "<p style=\"margin-left: 6em;\">".nl2br(stripslashes($rowcomment['Comment']))."</p></div>";
                        
                            echo "<p class=\"author\"><span>".commentNames($rowcomment['FirstName'], $rowcomment['Surname']).commentCountry($rowcomment['CityTown'], $rowcomment['Country'])."</span>".date('l, F d, Y', strtotime(str_replace("&", "&amp;", $rowcomment['DateAdded'])))."</p></li>";
                        
                        
                        }
                        /*<li class="ieclear">
                         <div>
                         <h3>I remember when we met</h3>
                         <img src="images/assets/teddybear.jpg" alt="" class="leftpic" />
                         <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
                         tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
                         quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
                         consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
                         cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
                         non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
                         Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
                         tempor incididunt ut labore. <a href="#" title="" class="morelink" >read more</a>
                         </p>
                         </div>
                         <p class="author"><span>Evan Phillips. Auckland, New Zealand.</span> Wednesday, 1 January 2009</p>
                         </li>*/
                        echo "</ul>
                                                                                                        </li>";
                         insertSearchPageBottomNavigation($_GET['personid'], $totalpages, $currentpage);
                        ?>
                    </ul>
                </li>
            </ul>
            <?php
            $publishedNotices = 0;
            $query2 = "select count(*) from Publication left join PublicationPerson on Publication.ID = PublicationPerson.PublicationID where PublicationPerson.PersonID = ".$_GET['personid'];
            $result2 = mysql_query($query2, $link);
            $query4 = "select `Name`, `DatePublished` from Publication left join PublicationPerson on Publication.ID = PublicationPerson.PublicationID where PublicationPerson.PersonID = ".$_GET['personid'];
            $result4 = mysql_query($query4, $link);
            if (mysql_num_rows($result2) != 0)
            {
                $publishedNotices = mysql_result($result2, 0);
            }
            $papers = array();
            if (mysql_num_rows($result4) != 0)
            {
                while ($row3 = mysql_fetch_assoc($result4))
                {
                    $pubname = explode("-", $row3['Name']);
                    //standard format is 'death-notice-thepress-06042009'
                    if (count($pubname) == 4)
                    {
                        $pubname = str_replace(" ", "", $pubname[2]);
                        $query5 = "select `Name` from Newspapers where `ShortName` = '".$pubname."';";
                        $result5 = mysql_query($query5, $link);
                        array_push($papers, mysql_result($result5, 0)." - ".formatdatetime4($row3['DatePublished']));
                    }
                }
            }
            $paperList = '';
            foreach($papers as $paper) {
                $paperList .= '<li>'.$paper.'</li>';
            }
            $personId = $_GET['personid'];
            $fullName = '';
            if (!empty($personId)) {
                $names = array($row['FirstNames'], strtoupper($row['Surname']));
                $fullName = implode(' ', array_filter($names));
            }
            echo <<< EOT
            <ul id="rightbar">
                <li class="linkitem">
                    <h2><a href="#">Published Notices ($publishedNotices)</a></h2>
                    <ul>
$paperList
                        <li class="right"><a href="personnotices.php?personid=$personId">view all</a></li>
                    </ul>
                </li>
                <li class="linkitem feature"><a href="leaveamessage.php?personid=$personId" title="Leave a Message">Leave a Message <span>Entries&nbsp;($entries)</span></a></li>
                <li class="linkitem"><a href="visitors.php?personid=$personId" title="View Visitor Index">View Visitor Index</a></li>
                <li class="linkitem">
                    <span class="new">&nbsp;</span>
                    <a href="/alerts.php" title="Book an Alert">Book an Alert</a>
                </li>
                <li class="linkitem">
                    <a href="gift_giving.php" title="Send a Gift">Send a Gift</a>
                </li>
                <li class="linkitem">
                    <a href="travel.php" title="Make Travel Arrangments">Make Travel Arrangements</a>
                </li>
                <li class="alt">
                    <a title="Tell a Friend" class="browsernav_tellfriend" href="mailto:?subject=A Memory Tree has a Remembrance Page open for $fullName&body=Hi, I'm letting you know that a Remembrance Page has been opened on http://www.amemorytree.co.nz website and you can leave a message for free today.%0D%0A%0D%0AVisit http://amemorytree.co.nz/remembrance.php?personid=$personId to view the page and read other messages.">Tell a Friend</a>
                </li>
            </ul>

EOT;
            ?>
        </div>
        <?php
        include ('view/standard-noads-bottom.php');
        ?>
